package com.example.myapplication.base.mvp.ui;

import com.example.myapplication.base.mvp.BasePresenter;
import com.example.myapplication.base.mvp.IBaseUiView;
import com.example.myapplication.utils.Toast;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.XPopupCallback;
import com.lxj.xpopup.util.LogUtil;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

public abstract class BaseFraction<P extends BasePresenter> extends Fraction implements IBaseUiView  {
    protected P mPresenter;
    protected Component mComponentView;
    public abstract int getUIContent();
    protected abstract P createPresenter();
    public abstract void initComponent();
    public abstract void getData();
    public void initClickListener(){}
    public Context mContext;

    private LoadingPopupView loadingPopup ;

    private void initLoadindDialog(){
        loadingPopup = (LoadingPopupView) new XPopup.Builder(getContext())
                .dismissOnBackPressed(true)
                .dismissOnTouchOutside(false)
                .setPopupCallback(new XPopupCallback() {
                    @Override
                    public void onCreated(BasePopupView basePopupView) {

                    }

                    @Override
                    public void beforeShow(BasePopupView basePopupView) {

                    }

                    @Override
                    public void onShow(BasePopupView basePopupView) {

                    }

                    @Override
                    public void onDismiss(BasePopupView basePopupView) {

                    }

                    @Override
                    public void beforeDismiss(BasePopupView basePopupView) {

                    }

                    @Override
                    public boolean onBackPressed(BasePopupView basePopupView) {
                        LogUtil.error("htd","取消");
                        return false;
                    }

                    @Override
                    public void onKeyBoardStateChanged(BasePopupView basePopupView, int i) {

                    }

                    @Override
                    public void onDrag(BasePopupView basePopupView, int i, float v, boolean b) {

                    }
                })
                .asLoading("加载中");


    }
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        mComponentView = scatter.parse(getUIContent(), container, false);
        mContext = getFractionAbility();
        initLoadindDialog();
        mPresenter = createPresenter();
        mPresenter.bindView(this);
        mPresenter.oncreate();
        initComponent();
        initClickListener();
        getData();
        return mComponentView;
    }
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }
    @Override
    protected void onActive() {
        super.onActive();
    }
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    public String getString(int resId) {
        try {
            return getFractionAbility().getResourceManager().getElement(resId).getString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public int getColor(int colorId) {
        try {
            return getFractionAbility().getResourceManager().getElement(colorId).getColor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void showLoadingDialog() {
        if(loadingPopup!=null&&!loadingPopup.isShow()){
            loadingPopup.show();
        }
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showErrorMsg(int errorCode, String msg) {
        showToast(msg);
    }

    @Override
    public void showToast(String message) {
        Toast.makeToast(mContext,message,3000).show();
    }


    @Override
    public void showLoadingDialogNow(boolean isNow) {
        if(isNow){
            showLoadingDialog();
        }else {
            if(loadingPopup != null && !loadingPopup.isShow()) {

//                postTask(new Runnable() {
//                    @Override
//                    public void run() {
//                        showLoadingDialog();
//                    }
//                }, 750);

            }
        }

    }
    @Override
    public void dismissLoadingDialog() {
        if(loadingPopup!=null)loadingPopup.dismiss();
    }
    @Override
    public void showErrorView() {

    }

    @Override
    public void showNoMoreView() {

    }
}
